package com.hh.base.common;

import com.hh.base.util.excel.TableRow;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by liuhanhui on 2017/10/27.
 */
public class TableUtil {

    public static List<TableBean> parseTables(Map<String, List<TableRow>> tableMap) {
        List<TableBean> tableBeans = new ArrayList<TableBean>();
        for (String key : tableMap.keySet()) {
            if (key.equals("README")) {
                continue;
            } else if (key.equals("PROJECT_ERROR_CODE")) {
                continue;
            }
            List<TableRow> tableRows = tableMap.get(key);
            TableBean tableBean = new TableBean();
            List<FiledBean> filedBeans = new ArrayList<FiledBean>();
            for (int i = 1; i < tableRows.size(); i++) {
                TableRow row = tableRows.get(i);
                FiledBean filedBean = new FiledBean();
                List<String> values = row.getValues();
                filedBean.setName(values.get(0));
                filedBean.setComment(values.get(1));
                filedBean.setType(values.get(2));
                String length = values.get(3);
                filedBean.setLength(Integer.parseInt("".equals(length) ? "0" : length));
                if (values.size() >= 5) {
                    String primary = values.get(4).toString();
                    filedBean.setIsPrimary(Integer.parseInt("".equals(primary) ? "0" : primary));
                }
                if (values.size() >= 6) {
                    String autoIn = values.get(5).toString();
                    filedBean.setIsAutoIncrement(Integer.parseInt("".equals(autoIn) ? "0" : autoIn));
                }
                filedBeans.add(filedBean);
            }
            tableBean.setName(key);
            tableBean.setFiledBeanList(filedBeans);
            tableBeans.add(tableBean);
        }
        return tableBeans;
    }
}
